# SETTINGS SECTION

PYWORDNET_PATH = '../../../misc'
DICT_PATH = '../../../misc/mzdict'
ENCODING = 'utf-8'
DELIMITER = '\t'

# END OF SETTINGS

import os, sys
if PYWORDNET_PATH not in sys.path:
	sys.path.append(PYWORDNET_PATH)
os.environ['WNSEARCHDIR'] = os.path.join(PYWORDNET_PATH, 'pywordnet/dict')
import pywordnet as wordnet


def search_WordNet(query):
	try:	# If the lookup word is not in ASCII range, it will raise exception
		query = str(query)
		result = ''
		if wordnet.V.has_key(query):	# If the word is a Verb
			result = '(v) ' + str(wordnet.V[query].getSenses()[0].synset.gloss)

		if wordnet.N.has_key(query):	# If the word is a Noun
			result += '\n' + '(n) ' + str(wordnet.N[query].getSenses()[0].synset.gloss)
	
		if wordnet.ADJ.has_key(query):	# If the word is an Adjective
			result += '\n' + '(adj) ' + str(wordnet.ADJ[query].getSenses()[0].synset.gloss)
	
		if wordnet.ADV.has_key(query):	# If the word is an Adverb
			result += '\n' + '(adv) ' + str(wordnet.ADV[query].getSenses()[0].synset.gloss)
	except StandardError:
		return None
	return result


import os
import codecs
import collections

# Load dictionary at startup
mainDict = collections.defaultdict(lambda : '')
for fileName in os.listdir(DICT_PATH):
	if fileName.endswith('.txt'):
		filePath = os.path.join(DICT_PATH, fileName)
		lineNum = 0
		print 'LOOKUP: reading', filePath
		for line in codecs.open(filePath, encoding=ENCODING).readlines():
			columns = line.strip().split(DELIMITER)
			lineNum += 1
			if len(columns) >= 3:
				word = columns[0].strip()
				category = columns[1].strip()
				description = columns[2].strip()
				mainDict[word] += '(%s) %s\n' % (category, description)
			else:
				print 'WARNING: invalid entry at line', lineNum
mainDictSize = len(mainDict.keys())
